package qeb.handheldmedical.adapter;

import org.json.JSONArray;
import org.json.JSONObject;

import qeb.handheldmedical.R;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class PatientAdapter extends BaseAdapter{

	private static final String TAG = "PatientAdapter";
	private boolean mBusy = false;

	public void setFlagBusy(boolean busy) {
		this.mBusy = busy;
	}
	
	//private ImageSwitcher mImageLoader;
	private int mCount;
	private Context mContext;
	private JSONArray patientJsonArray =null;
	
	public PatientAdapter(int count, Context context, JSONArray patientJsonArray) {
		this.mCount = count;
		this.mContext = context;
		this.patientJsonArray = patientJsonArray;
		//mImageLoader = new ImageSwitcher(context);
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mCount;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewholder = null;
		if(convertView==null)
		{
			convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_inpatient, null);
			viewholder = new ViewHolder();
			viewholder.imgPatient = (ImageView)convertView.findViewById(R.id.imgPatient);
			viewholder.txtch = (TextView)convertView.findViewById(R.id.txtch);
			viewholder.txtbah = (TextView)convertView.findViewById(R.id.txtBah);
			viewholder.txtPatientname = (TextView)convertView.findViewById(R.id.txtPatientname);
			viewholder.txtPatientinfo = (TextView)convertView.findViewById(R.id.txtPatientinfo);
			viewholder.txtHiddpatientinfo = (TextView)convertView.findViewById(R.id.txtHiddpatientinfo);
			convertView.setTag(viewholder);
		}
		else
		{
			viewholder = (ViewHolder)convertView.getTag();
		}
		if (!mBusy) {
			try {
				JSONObject patients = patientJsonArray.getJSONObject(position);
				if(patients!=null)
				{
					viewholder.txtch.setText(" "+patients.getString("CH"));
					viewholder.txtbah.setText(patients.getString("BAH"));
					viewholder.txtPatientname.setText(patients.getString("XM"));
					viewholder.txtPatientinfo.setText(" "+patients.getString("ZD"));
					if(patients.getString("SEXID").equals("0"))
						viewholder.imgPatient.setBackgroundResource(R.drawable.nan01);
					else
						viewholder.imgPatient.setBackgroundResource(R.drawable.nv01);
					//viewholder.txtHiddpatientinfo.setText(urlArrays[position]);
				}
			} catch (Exception e) {
				// TODO: handle exception
				Log.e(TAG, e.toString());
			}
		} else {
			//mImageLoader.DisplayImage(url, viewHolder.mImageView, false);	
			viewholder.txtch.setText("");
			viewholder.txtbah.setText("");
			viewholder.txtPatientname.setText("");
			viewholder.txtPatientinfo.setText("");
			viewholder.txtHiddpatientinfo.setText("");
			//viewholder.txtPatientinfo.setText("诊断：心脏病          入院时间：2013-05-06" + position);
		}
		return convertView;
	}
	
	static class ViewHolder {
		ImageView imgPatient;
		TextView txtch;
		TextView txtbah;
		TextView txtPatientname;
		TextView txtPatientinfo;
		TextView txtHiddpatientinfo;
	}
}

